package l1j.opqlo.NewSystem.寄賣系統;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.SQLUtil;

public class AutoShopTable {


	private static Logger _log = Logger.getLogger(AutoShopTable.class.getName());
	private static AutoShopTable _instance;
	private final ArrayList<AutoShop> _AutoShopList = new ArrayList<AutoShop>();
	private static int version =0;
	public static AutoShopTable getInstance(){
		if(_instance ==null){
			_instance = new AutoShopTable();
		}
		return _instance;
	}


	private final ArrayList<AutoShop>_list = new ArrayList<AutoShop>();


	private int _totalprice;

	public synchronized void add(final int orderNumber, int count){
		final AutoShopTable autoshop = AutoShopTable.getInstance();
		final ArrayList<AutoShop>shopList = autoshop.getAutoShopList();
		if(orderNumber > shopList.size()){
			return;
		}

		final AutoShop shopitem = shopList.get(orderNumber);
		final int price = shopitem.getPrice();
		//opqlo 防洗
		final int set_count = 2000000000 / price;
		if(count > set_count){return;}
		//opqlo 防洗
		if(count >1 && shopitem.getCount()<count ){
			count = shopitem.getCount();
		}
		this._totalprice += count * price;

		this._list.add(shopitem);

	}
	public synchronized void addShopItem(final String AccountName ,final L1ItemInstance item , final int price){
		final int itemid = item.getItemId();
		final L1Item itemTemplate = ItemTable.getInstance().getTemplate(itemid);
		if (itemTemplate == null) {
			_log.warning(String.format("item id:%d not found", itemid));
			return;
		}
		final AutoShop shoplist =	new AutoShop(
				AccountName,
				item.getId(),
				itemid,
				itemTemplate,
				item.getCount(),
				item.getEnchantLevel(),
				item.isIdentified() != false ? 0 : 1,
				item.get_durability(),
				item.getChargeCount(),
				item.getRemainingTime(),
				item.getBless(),
				item.getAttrEnchantKind(),
				item.getAttrEnchantLevel(),
				item.getFireMr(),
				item.getWaterMr(),
				item.getEarthMr(),
				item.getWindMr(),
				item.getaddSp(),
				item.getaddHp(),
				item.getaddMp(),
				item.getHpr(),
				item.getMpr(),
				item.getSkillType(),
				item.getLevel(),
				price
		);
		this._AutoShopList.add(shoplist);

		this.AddVersion();
	}

	private synchronized void AddVersion(){
		version++;
	}

	public synchronized ArrayList<AutoShop> getAutoShopList(){

		return this._AutoShopList;
	}


	public synchronized int GetVersion(){
		return version;
	}




	public synchronized void loadDate(){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM Z_opqlo_AutoShop");
			rs = pstm.executeQuery();
			this.LoadShop(rs);
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, "error while creating Z_opqlo_AutoShop table",e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	private synchronized void LoadShop(final ResultSet rs) throws SQLException {
		while (rs.next()) {
			final int itemid = rs.getInt("道具編號");
			final L1Item itemTemplate = ItemTable.getInstance().getTemplate(itemid);
			if (itemTemplate == null) {
				_log.warning(String.format("item id:%d not found", itemid));
				return;
			}
		final AutoShop shoplist =	new AutoShop(
					rs.getString("帳號"),
					rs.getInt("流水號") ,
					itemid,
					itemTemplate,
					rs.getInt("數量"),
					rs.getInt("強化數"),
					rs.getInt("isid"),
					rs.getInt("損壞值"),
					rs.getInt("charge_count"),
					rs.getInt("使用時間"),
					rs.getInt("bless"),
					rs.getInt("屬性類型"),
					rs.getInt("屬性等級"),
					rs.getInt("抗火"),
					rs.getInt("抗水"),
					rs.getInt("抗地"),
					rs.getInt("抗風"),
					rs.getInt("魔攻"),
					rs.getInt("HP"),
					rs.getInt("MP"),
					rs.getInt("Hpr"),
					rs.getInt("Mpr"),
					rs.getInt("SkillType"),
					rs.getInt("等級"),
					rs.getInt("售價"));
		this._AutoShopList.add(shoplist);
		}


	}



	public synchronized void removeShopItem(final int id){
		this._AutoShopList.remove(id);
		this.AddVersion();
		}


	public synchronized void sellitem(final L1PcInstance pc){
		final L1ItemInstance finditem = pc.getInventory().findItemId(90308);
		if(finditem == null || finditem.getCount() <this._totalprice){
			pc.sendPackets(new S_SystemMessage("商城元寶 數量不足。"));
			return;
		}else{
			pc.getInventory().removeItem(finditem, this._totalprice);
		}
		this._AutoShopList.removeAll(this._list);
		for(int i=0; i<this._list.size(); i++){
		pc.getInventory().storeTradeItem(this._list.get(i).getIteminstance());
		this._list.remove(i);
		}
		this.AddVersion();


	}


}
